package com.example.exam.controller;

import com.example.exam.pojo.Book;
import com.example.exam.pojo.BookPager;
import com.example.exam.service.imp.BookService;
import com.example.exam.util.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;


    @ApiOperation(value = "添加书本", notes = "添加书本")
    @PostMapping("/insertBook")
    public RespBean insertBook(@RequestBody @Valid  Book book) {
        bookService.insertBook(book);
        return RespBean.ok("书本插入成功");

    }

    @ApiOperation(value = "更新书本", notes = "更新书本")
    @PostMapping("/updateBookById")
    public RespBean updateBookById(@RequestBody @Valid Book book) {
         bookService.updateBookById(book);
        return RespBean.ok("书本更新成功");

    }

    @ApiOperation(value = "查询书本", notes = "分页查询")
    @GetMapping("/selectBookByLimit")
    public RespBean selectBookByLimit(Long row, Long number) {

       BookPager  bookPager = bookService.selectBookByLimit(row,number);

        return RespBean.ok("查询结果",bookPager);
    }


    @ApiOperation(value = "删除书本", notes = "以编号删除书本")
    @GetMapping("/deleteBookById")
    public RespBean deleteBookById(Long id) {
        bookService.deleteBookById(id);
        return RespBean.ok("书本删除成功");
    }

}
